import { defineStore } from "pinia";
import { login } from '@/api/login.js'
import auth from '@/utils/auth.js'
import cache from '@/utils/cache.js'
import { reject } from "lodash";
export const useUserStore = defineStore('user', {
    state: () => {
        return {
            token: '',
            userInfo: {},
            authInfo: {}
        }
    },
    getters: {},
    actions: {
        userLogin(data){
            return new Promise((resolve, reject) => {
                login(data).then((res) => {
                    console.log('登录的用户信息', res);
                    auth.setToken(res.data.token)
                    this.token = res.data.token
                    this.userInfo = res.data.userInfo
                    resolve('loginSuccess')
                }).catch(err => {
                    reject('error')
                })
            })
        },
        async logOut() {
            this.token = ''
            this.userInfo = {}
            this.authInfo = {}
            auth.removeToken()
            cache.removeSession('sessionObj')
            cache.removeLocalstorage('authInfo')
            cache.removeSession('q_async_routes')
        }
    },
    persist: {
        enabled: true, // 开启数据缓存
        strategies: [
            {
                key: "authInfo",
                storage: sessionStorage,
                paths: ["authInfo"],
            },
        ]
    }
})
